﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Enum;
using RuleCode.Table;

namespace map.manage
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind1();
                bind2();
                bind3();
                GridViewBind();
            }
        }
        /// <summary>
        /// 县
        /// </summary>
        private void bind1()
        {
            string sheng = zfunction.Province;
            RuleCode.Table.县 x = RuleCode.Table.县.Find(" 省编码 = '" + sheng + "' ");
            DropDownList1.Items.Clear();
            foreach (DataRow item in x.MyTable.Rows)
            {
                DropDownList1.Items.Add(item["县编码"].ToString());
            }
            string ScriptTag = "";
            ScriptTag =
                    "<script type=\"text/javascript\">markpos(" + "116.39679908752441" + ","
                    + "" + "39.917043746896674,'" + "2" + "');</script>";
            ClientScript.RegisterStartupScript(GetType(), "bodyevent", ScriptTag);
            //DropDownList1.DataBind();
        }
        /// <summary>
        /// 区
        /// </summary>
        private void bind2()
        {
            string xian = DropDownList1.Text;
            RuleCode.Table.区 x = RuleCode.Table.区.Find(" 县编码 = '" + xian + "' ");
            DropDownList2.Items.Clear();
            foreach (DataRow item in x.MyTable.Rows)
            {
                DropDownList2.Items.Add(item["区编码"].ToString());

            }
            if (!IsPostBack)
            {
                string ScriptTag = "";
                if (x.taobaolong.Value == 0 || x.taobaolat.Value == 0)
                {
                    //ScriptTag =
                    //    "<script type=\"text/javascript\">markpos(" + "116.396" + ","
                    //    + "" + "39.917,'" + x.区编码.ToString() + "');</script>";
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos(" + "116.39679908752441" + ","
                        + "" + "39.917043746896674,'" + x.区编码.ToString() + "');</script>";
                }
                else
                {
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos(" + x.taobaolong.ToString() + ","
                        + x.taobaolat.ToString() + ",'" + x.区编码.ToString() + "');</script>";
                }
                ClientScript.RegisterStartupScript(GetType(), "bodyevent", ScriptTag);
            }
        }

        /// <summary>
        /// 区域表格
        /// </summary>
        private void bind3()
        {
            RuleCode.Table.区域 x = RuleCode.Table.区域.Find(" 区编码 = '" + DropDownList2.Text + "' "
                + " and 县编码 = '" + DropDownList1.Text + "' ");
            DropDownList3.DataSource = x.MyTable.DefaultView;
            DropDownList3.DataTextField = "区域编码";
            DropDownList3.DataValueField = "区域编码";
            DropDownList3.DataBind();
            if (!IsPostBack)
            {
                string ScriptTag = "";
                if (x.taobaolong.Value == 0 || x.taobaolat.Value == 0)
                {
                    //ScriptTag =
                    //    "<script type=\"text/javascript\">markpos(" + "116.396" + ","
                    //    + "" + "39.917,'" + x.区编码.ToString() + "');</script>";
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos(" + "116.39679908752441" + ","
                        + "" + "39.917043746896674,'" + x.区域编码.ToString() + "');</script>";
                }
                else
                {
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos(" + x.taobaolong.ToString() + ","
                        + x.taobaolat.ToString() + ",'" + x.区域编码.ToString() + "');</script>";
                }
                ClientScript.RegisterStartupScript(GetType(), "bodyevent", ScriptTag);
            }

        }


        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            bind2();
            bind3();
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            bind3();
        }
        //todo:是否删，先不做删除
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            BizArea uc = BizArea.Find(" recid = '" + id + "'", "RECID");
            uc.Delete();
            GridViewBind();
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string id = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
            BizArea uc = BizArea.Find(" recid = '" + id + "'", "RECID");

          
            string ScriptTag = "";
            if (uc.taobaolong.Value == 0 || uc.taobaolat.Value == 0)
            {
                ScriptTag =
                    "markpos2(" + "" + "116.39679908752441,"
                    + "39.917043746896674" + ",'" + uc.BizAreaName.ToString() + "')";
            }
            else
            {
                ScriptTag =
                    "markpos2(" + uc.taobaolong.ToString() + ","
                    + uc.taobaolat.ToString() + ",'" + uc.BizAreaName.ToString() + "')";
            }
            ScriptManager.RegisterStartupScript(
                this.UpdatePanel1, this.UpdatePanel1.GetType(), "bodyevent3", ScriptTag, true);
        }

      

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewBind();
        }

        private void GridViewBind()
        {
            RuleCode.Table.BizArea x = RuleCode.Table.BizArea.Find(" 区编码 = '" + DropDownList2.Text + "' "
                                                                   + " and 县编码 = '" + DropDownList1.Text + "' " + " and 区域编码 = '" + DropDownList3.Text + "' ");
            this.GridView1.DataSource = x.MyTable;
            //foreach (DataRow s in x.MyTable.Rows)
            //{
            //    s["IsConfirmed"] = s["IsConfirmed"].ToString().Equals("0") ? "False" : "True";
               
            //    //(s[0].ToString().Equals("0"))? s[0] = "False" :
            //}
            //x.MyTable.Columns["IsConfirmed"].Table.Rows.GetEnumerator()
            this.GridView1.DataBind();
            if (!IsPostBack)
            {
                string ScriptTag = "";
                if (x.taobaolong.Value == 0 || x.taobaolat.Value == 0)
                {
                    //ScriptTag =
                    //    "<script type=\"text/javascript\">markpos(" + "116.396" + ","
                    //    + "" + "39.917,'" + x.区编码.ToString() + "');</script>";
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos2(" + "116.39679908752441" + ","
                        + "" + "39.917043746896674,'" + x.区编码.ToString() + "');</script>";
                }
                else
                {
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos2(" + x.taobaolong.ToString() + ","
                        + x.taobaolat.ToString() + ",'" + x.区编码.ToString() + "');</script>";
                }
                ClientScript.RegisterStartupScript(GetType(), "bodyevent", ScriptTag);

            }
            else
            {
               
                string ScriptTag =
                     "<script type=\"text/javascript\">markpos2(" + x.taobaolong.ToString() + ","
                     + x.taobaolat.ToString() + ",'" + x.区编码.ToString() + "');</script>";

                ScriptManager.RegisterStartupScript(
                this.UpdatePanel1, this.UpdatePanel1.GetType(), x.taobaolong.ToString() + x.taobaolat.ToString(), ScriptTag, false);
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            //审核通过
            string id = GridView1.DataKeys[e.NewEditIndex].Value.ToString();
            BizArea uc = BizArea.Find(" recid = '" + id + "'", "RECID");
            uc.IsConfirmed = NoYes.Yes;
            uc.Modify();
            GridViewBind();
            


        }

        public string GetBindCheckBoxHtml(string IsConfirmed)
        {
            string result = string.Empty;
            //result = "<input  type=\"checkbox\"  checked=\"" + ((IsConfirmed == "1") ? "true" : "false") + "\"/>"; //"<asp:CheckBox runat=\"server\" Checked=\""+((IsConfirmed=="1")?"true":"false")+"\" />";
            result = "<span>"+((IsConfirmed == "1") ? "是" : "否")+"</>"; 
            return result;
        }
    }
}
